package com.example.demo.commons.core.util;

import cn.hutool.core.util.ReflectUtil;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * <p> 对象工具类 </p>
 *
 * @author tanyunpeng
 * @date 2021/11/18
 */
public class LocalObjectUtils {

    public static boolean anyNull(Object... objectList) {
        if (Objects.isNull(objectList)) {
            return true;
        }
        for (Object obj : objectList) {
            if (Objects.isNull(obj)) {
                return true;
            }
        }
        return false;
    }

    public static boolean allNull(Object... objectList) {
        if (Objects.isNull(objectList)) {
            return true;
        }
        for (Object obj : objectList) {
            if (Objects.nonNull(obj)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断入参列表全部不为NULL
     *
     * @param objectList 入参列表
     * @return 全部不为NULL
     */
    public static boolean notNull(Object... objectList) {
        if (Objects.isNull(objectList)) {
            return false;
        }
        for (Object obj : objectList) {
            if (Objects.isNull(obj)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断PoJo属性是否存在NULL值
     *
     * @param pojo                 pojo对象
     * @param withSuperClassFields 是否判断父类
     * @return 是否存在为空
     */
    public static boolean pojoFieldsAnyNull(Object pojo, boolean withSuperClassFields) throws IllegalAccessException {
        if (Objects.isNull(pojo)) {
            return true;
        }
        for (Field field : ReflectUtil.getFieldsDirectly(pojo.getClass(), withSuperClassFields)) {
            field.setAccessible(true);
            if (Objects.isNull(field.get(pojo))) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断Pojo属性是否全为空
     *
     * @param pojo                 pojo对象
     * @param withSuperClassFields 是否判断父类
     * @return 是否全为空
     */
    public static boolean pojoFieldsAllNull(Object pojo, boolean withSuperClassFields) throws IllegalAccessException {
        if (Objects.isNull(pojo)) {
            return true;
        }
        for (Field field : ReflectUtil.getFieldsDirectly(pojo.getClass(), withSuperClassFields)) {
            field.setAccessible(true);
            if (Objects.nonNull(field.get(pojo))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断PoJo对象属性是否全非空
     *
     * @param pojo                 pojo对象
     * @param withSuperClassFields 是否判断父类
     * @return 是否全非空
     */
    public static boolean pojoFieldsNotNull(Object pojo, boolean withSuperClassFields) throws IllegalAccessException {
        if (Objects.isNull(pojo)) {
            return false;
        }
        for (Field field : ReflectUtil.getFieldsDirectly(pojo.getClass(), withSuperClassFields)) {
            field.setAccessible(true);
            if (Objects.isNull(field.get(pojo))) {
                return false;
            }
        }
        return true;
    }
}
